package com.enlorenz.core.channel.activemq.common;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 公用MQ
 * @author MAC
 */
public class CommMQUtil {
	private static final String maxInactivityDuration = "?wireFormat.maxInactivityDuration=0";
	//链接
	private Connection connection; 
	//通道
	private Destination destination; 
	//登陆用户名
	private String userName;
	//登陆密码
	private String userPwd;
	//MQ服务地址
	private String mqUrl;
	//会话
	private Session session; 
	//是否开启事物
	private boolean isTransacted = false; 
	//创建通道类型
	private boolean isTopic=false;
	
	public void createConnection() throws JMSException{
		ActiveMQConnectionFactory connectionFactory =
			new ActiveMQConnectionFactory(userName, userPwd,mqUrl + maxInactivityDuration);
		connection = connectionFactory.createConnection();
	}
	
	public void createSession() throws JMSException{
		session = connection.createSession(isTransacted, Session.AUTO_ACKNOWLEDGE);
	}
	
	public void createDestination(String destName) throws JMSException{
		if (isTopic){
			destination = session.createTopic(destName);
		}
		else{
			destination = session.createQueue(destName);
		}
	}
}
